package com.embracesource.monitor.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.embracesource.monitor.entity.Product;
import com.embracesource.monitor.service.IProductService;
import com.gnerv.pabu.basic.core.entity.Result;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 基础平台-产品表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-09-18
 */
@RestController
@RequestMapping("/api/monitor/product")
public class ProductController {

    @Resource
    private IProductService productService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody Product product) {
        productService.saveProduct(product);
        return Result.response();
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result create(@RequestBody Product product) {
        productService.createProduct(product);
        return Result.response();
    }

    @RequestMapping(value = "/delete/{productId}", method = RequestMethod.GET)
    public Result delete(@PathVariable(value = "productId") String productId) {
        productService.deleteProduct(productId);
        return Result.response();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Product product) {
        productService.updateProduct(product);
        return Result.response();
    }

    @RequestMapping(value = "/select/{productId}", method = RequestMethod.GET)
    public Result select(@PathVariable(value = "productId") String productId) {
        Product product = productService.selectProduct(productId);
        return Result.response().data("product", product);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(Product product,
                       @RequestParam(value = "current", defaultValue = "1") Long current,
                       @RequestParam(value = "size", defaultValue = "300") Long size){
        Page<Product> products = productService.listProduct(product, current, size);
        return Result.response().data("products", products);
    }

}
